#pragma once 
#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;

private:
    void OnMessage(const PtrConnection &conn, Buffer *buf)
    {
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
    }
    void OnClosed(const PtrConnection &conn)
    {
        INF_LOG("CLOSE CONNECTION :%p", conn.get());
    }
    void OnConnected(const PtrConnection &conn)
    {
        INF_LOG("NEW CONNECTION :%p", conn.get());
    }

public:
    EchoServer(uint16_t port) : _server(port)
    {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCalllback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCalllback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _server.SetClosedCalllback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
    }

    void Start()
    {
        _server.Start();
    }
};